package cn.dengta.webapp.live.entity;

import java.util.Date;
import java.util.concurrent.TimeUnit;

import cn.dengta.webapp.base.entity.BaseEntity;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

/**
 * 直播间连麦记录表
 *
 * @author zw
 * @since 2023-01-05
 */
@Setter
@Getter
@Accessors(chain = true)
@TableName("im_live_interact_log")
public class LiveInteractLog extends BaseEntity {

	private static final long serialVersionUID = 1L;

	/** 直播id */
	Long liveId;
	/** 直播间主播用户id */
	Long anchorUserId;
	/** 用户id */
	Long userId;
	/** 1=观众发起；2=主播发起; 默认1 */
	Integer type;
	/** 创建时间 */
	Date addTime;
	/** 结束时间 */
	Date endTime;
	/** 直播状态：-3=直播关闭连麦功能；-2=拒绝连麦；-1=取消连麦；0=等待中；1=连麦中; 2=连麦结束 */
	Integer status;
	/** 连麦开始时间 */
	Date micBeginTime;
	/** 连麦结束时间 */
	Date micEndTime;
	/** 直播次数 */
	transient Integer liveCount;
	/** 直播人数 */
	transient Integer liveUserCount;
	/** 直播时长 */
	transient Long duration;

	public static LiveInteractLog of(Integer type, Live live, Long userId, Date now) {
		LiveInteractLog log = new LiveInteractLog();
		log.setType(type);
		log.setLiveId(live.getId());
		log.setAnchorUserId(live.getUserId());
		log.setUserId(userId);
		log.setAddTime(now);
		log.setStatus(0);
		return log;
	}

	/** 直播时间（秒） */
	public long getDuration() {
		return micBeginTime != null && micEndTime != null ? TimeUnit.MILLISECONDS.toMinutes(micEndTime.getTime() - micBeginTime.getTime()) : 0L;
	}

	public static LiveInteractLog of(Live live, Long userId, Date now) {
		return of(1, live, userId, now);
	}

	/** 是否等待中 */
	public boolean hasWait(){
		return this.status == 0;
	}

	public static final String LIVE_ID = "live_id";
	public static final String USER_ID = "user_id";
	public static final String TYPE = "type";
	public static final String ANCHOR_USER_ID = "anchor_user_id";
	public static final String ADD_TIME = "add_time";
	public static final String END_TIME = "end_time";
	public static final String STATUS = "status";
	public static final String MIC_BEGIN_TIME = "mic_begin_time";
	public static final String MIC_END_TIME = "mic_end_time";

}
